using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Windows.Forms;

namespace BGSpaceShooter
{
    public class MainGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public Router Router { get; set; }

        /// <summary>
        /// Ustala glowny katalog contentu i tworzym GDM
        /// </summary>
        public MainGame()
        {
            IsMouseVisible = true;

            graphics = new GraphicsDeviceManager(this);


            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Inicjalizuje obiekty gry, menadzer contentu
        /// </summary>
        protected override void Initialize()
        {
            GameContentManager.Initialize(Content);
            TechniqueProvider.StartTechniqueProvider(Content);
            //ServerAddress dialogForm = new ServerAddress();

            //if (dialogForm.ShowDialog() == DialogResult.OK)
                Router = new Router(graphics); //Nie moze byc w konstruktorze, poniewaz dopiero w Initialize istnieje obiekt GraphicsDevice
            //else
            //    Exit();
            base.Initialize();
        }


        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Creation a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non GameContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (Router != null)
                Router.Update(gameTime);

            if (Router.GameState == GameState.End)
            {
                Exit();
            }

            base.Update(gameTime);
        }

        /// <summary>
        /// Prosi router o rozpoczecie rysowania, generalnie niczego wiecej robicz nie powinno
        /// </summary>
        /// <param name="gameTime">czas gry, aktualnie nie wykorzystany, moze sie przydac do shaderow (animacje w update?)</param>
        protected override void Draw(GameTime gameTime)
        {
            Router.Draw();
            base.Draw(gameTime);
        }

        protected override void OnExiting(object sender, EventArgs args)
        {
            base.OnExiting(sender, args);
            if(Router!=null)
            {
                Router.Stop();
            }
        }
        
    }
}
